package br.com.fox.iebav.dao;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.Query;

import br.com.fox.iebav.dao.abstractdao.AbstractDAO;
import br.com.fox.iebav.entity.Usuarios;



@Stateless
public class UsuariosDAO extends AbstractDAO  {

	private Query q = null;

	public List<Usuarios> findAll()
	{
		q = entityManager.createQuery("SELECT u FROM Usuarios u ORDER BY u.nome ASC");
		List<Usuarios> list = q.getResultList();
		if(list.isEmpty())
			return new ArrayList<Usuarios>();
		return list;
	}
		
	public Usuarios login(String log,String senha)
	{
		q = entityManager.createQuery("SELECT c FROM Usuarios c WHERE " +
				"c.login = :log AND " +
				"c.senha = :senha");
		
		q.setParameter("log", log);
		q.setParameter("senha", senha);
		List<Usuarios> list = q.getResultList();
		if(list.isEmpty())
			return null;
		else
		{
			Usuarios u = list.get(0);
			return u;
		}
	}
	
	public boolean verifyNameCadUser(String nome)
	{
		boolean res = false;
		
		q = entityManager.createQuery("SELECT u FROM Usuarios u WHERE " +
			"u.nome = :nome");
		
		q.setParameter("nome", nome);
		
		if(q.getResultList().isEmpty())
			res = true;
		return res;
	}
	
	public boolean verifyLogCadUser(String nomeAcesso)
	{
		boolean res = false;
	
		q = entityManager.createQuery("SELECT u FROM Usuarios u WHERE " +
				"u.login = :nome");
			q.setParameter("nome", nomeAcesso);
		
		if(q.getResultList().isEmpty())
			res = true;
		return res;
	}
}
